package com.ruoyi.web.controller.video;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.util.VideoUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

import static com.ruoyi.common.base.AjaxResult.error;
import static com.ruoyi.common.base.AjaxResult.success;

/**
 * @author 猪十三少
 * @date 2018/11/4 16:32
 */
@Controller
@RequestMapping("/video/video")
public class VideoController {
    private static final Logger log = LoggerFactory.getLogger(VideoController.class);
    private String prefix = "video/video";

    /**
     * 新增视频
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存视频
     */
    @RequiresPermissions("video:video:add")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("video") MultipartFile video) {
        try {
            if (!video.isEmpty()) {
                String url = VideoUploadUtils.upload(Global.getProfile(), video, ".avi", true);
                return success(url);
            }
            return error();
        } catch (Exception e) {
            log.error("上传视频失败！", e);
            return error(e.getMessage());
        }
    }


    /**
     * 删除视频
     */
    @RequiresPermissions("video:video:remove")
    @Log(title = "视频", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String url) {
        File video = new File(url);
        if (video.exists()) {
            boolean result = video.delete();
            if (result) {
                return success("删除视频成功");
            }else {
                return error();
            }
        }else {
            return error("该视频不存在");
        }
    }

    /**
     * 预览视频
     */
//    @RequiresPermissions("video:video:preview")
    @Log(title = "视频", businessType = BusinessType.PREVIEW)
    @GetMapping("/preview")
    @ResponseBody
    public void preview(String videoUrl, HttpServletResponse response) throws IOException {
        response.setHeader("Accept-Ranges", "bytes");
        response.setContentType("video/mp4");
        response.setHeader("Content-Disposition", "attachment; filename=1.mp4" );

        String separator = File.separator;
        if (separator.equalsIgnoreCase("\\")) {
            separator = "\\\\";
        }else {
            separator = "/";
        }
        videoUrl = videoUrl.replaceAll("\\@", ":").replaceAll("-", separator);
        System.out.println(videoUrl);
        FileUtils.writeBytes(videoUrl, response.getOutputStream());
    }
}
